package hxs.extras;

import flash.display.DisplayObject;
import flash.display.InteractiveObject;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.EventDispatcher;

#if flash
import flash.media.Microphone;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.Socket;
import flash.net.URLStream;
import flash.net.XMLSocket;

import hxs.events.flash.media.MicrophoneEvents;
import hxs.events.flash.net.FileReferenceEvents;
import hxs.events.flash.net.FileReferenceListEvents;
import hxs.events.flash.net.NetConnectionEvents;
import hxs.events.flash.net.NetStreamEvents;
import hxs.events.flash.net.SocketEvents;
import hxs.events.flash.net.URLStreamEvents;
import hxs.events.flash.net.XMLSocketEvents;
#end

import flash.media.Sound;
import flash.media.SoundChannel;



import flash.net.SharedObject;

import flash.net.URLLoader;


import flash.text.TextField;
import flash.utils.Timer;
import hxs.events.flash.display.DisplayObjectEvents;
import hxs.events.flash.display.InteractiveObjectEvents;
import hxs.events.flash.display.LoaderInfoEvents;
import hxs.events.flash.display.StageEvents;

import hxs.events.flash.media.SoundChannelEvents;
import hxs.events.flash.media.SoundEvents;


import hxs.events.flash.net.SharedObjectEvents;

import hxs.events.flash.net.URLLoaderEvents;

import hxs.events.flash.text.TextfieldEvents;
import hxs.events.flash.utils.TimerEvents;
import hxs.extras.FlashEventsBase;

#if flash
typedef ObjectHash<T1, T2> = flash.utils.TypedDictionary<T1, T2>;
#else 
import nme.ObjectHash;
#end

/**
 * ...
 * @author Tony Polinelli
 */

class FlashEventsStore 
{
	public static var objects:ObjectHash<Dynamic, FlashEventsBase> = new ObjectHash();
	
	public static function getEvents(object:Dynamic)
	{
		if (!objects.exists(object))
		{
			var storeType = findType(object);
			
			if (storeType == null)
				throw( "[" + object + "] is not a valid type to access HXS Shortcut Events");
				
			objects.set(object, Type.createInstance(storeType, [object]));
		}

		return objects.get(object);
	}
	
	private static function findType(object:Dynamic)
	{
		var validTypes = 	[DisplayObject, 		InteractiveObject, 			Stage,			LoaderInfo, 		SoundChannel,		Sound,			SharedObject,		TextField, 			Timer,			URLLoader		];
		var map =			[DisplayObjectEvents,	InteractiveObjectEvents,	StageEvents,	LoaderInfoEvents,	SoundChannelEvents,	SoundEvents,	SharedObjectEvents,	TextfieldEvents,	TimerEvents,	URLLoaderEvents	];
		
		// flash only
		#if (flash && !nme)
		var validTypes_flash =	[Microphone,		FileReference,			FileReferenceList,		NetConnection,			NetStream,			Socket,			URLStream,			XMLSocket];
		var map_flash =			[MicrophoneEvents,	FileReferenceEvents,	FileReferenceEvents,	NetConnectionEvents,	NetStreamEvents,	SocketEvents,	URLStreamEvents,	XMLSocketEvents];
		
		validTypes.concat(validTypes_flash);
		map.concat(map_flash);
		
		#end
		
		var type:Class<Dynamic> = Type.getClass(object);
		while (type != null)
		{
			if (Lambda.has(validTypes, type))
				break;
			type = Type.getSuperClass(type);
		}
		
		return map[Lambda.indexOf(validTypes, type)];
	}
}

